import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';

import 'default_bottom_sheet.dart';

/// 添加折扣
/// delete  返回 true
/// ok 返回对应的折扣
showAddDiscountBottomSheet(BuildContext context, String value) async {
  final result = await showDefaultBottomSheet(context,
      child: AddDiscountBottomSheet(
        value: value,
      ),
      title: S.current.add_discount,
      isScrollControlled: true,
      leading: DefaultBottomSheetLeadingButton(
        title: S.current.delete,
        onTap: value == null
            ? null
            : () {
                Navigator.of(context).pop(true);
              },
      ));
  return result;
}

class AddDiscountBottomSheet extends StatefulWidget {
  final String value;

  const AddDiscountBottomSheet({Key key, this.value}) : super(key: key);

  @override
  _AddDiscountBottomSheetState createState() => _AddDiscountBottomSheetState();
}

class _AddDiscountBottomSheetState extends State<AddDiscountBottomSheet> {
  TextEditingController discountController = TextEditingController();
  FocusNode discountFocusNode = FocusNode();
  bool isCheckPercentage = false;

  void _onChangePercentage() {
    setState(() {
      isCheckPercentage = !isCheckPercentage;
    });
    if (isCheckPercentage) {
      discountController.text = '0';
    } else {
      discountController.text = '0';
    }
  }

  bool isEnable = false;

  void _onAdd() {
    if (discountController.text.length == 0) {
      BaseConfig.dialogBase.showToast(
          context: context, content: S.current.please_enter_the_discount);
      return;
    }
    String returnText =
        '${discountController.text}${isCheckPercentage == true ? '%' : ''}';
    Navigator.of(context).pop(returnText);
  }

  @override
  void initState() {
    super.initState();
    if (widget.value != null) {
      if (widget.value.endsWith('%')) {
        isCheckPercentage = true;
        discountController.text = widget.value.replaceAll('%', '');
      } else {
        discountController.text = widget.value;
      }
    }
    discountController.addListener(() {
      if (discountController.text.length > 0) {
        if (isEnable == false) {
          setState(() {
            isEnable = true;
          });
        }
      } else {
        if (isEnable == true) {
          setState(() {
            isEnable = false;
          });
        }
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    discountController.dispose();
    discountFocusNode.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        AppSpaces.h20,
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            PressedOpacityButton(
              onPressed: _onChangePercentage,
              child: NormalRadio(
                title: S.current.flat_amount,
                isSelect: !isCheckPercentage,
              ),
            ),
            PressedOpacityButton(
              onPressed: _onChangePercentage,
              child: NormalRadio(
                title: S.current.percentage,
                isSelect: isCheckPercentage,
              ),
            ),
          ],
        ),
        AppSpaces.h20,
        InputField(
          tip: '${S.current.discount}${isCheckPercentage == true ? '(%)' : ''}',
          controller: discountController,
          focusNode: discountFocusNode,
          inputFormatters: [
            if (isCheckPercentage)
              percentInputFormatter2
            else
              doubleLimit2InputFormatter
          ],
        ),
        AppSpaces.h30,
        NormalButton(
          title: S.current.add,
          onTap: isEnable ? _onAdd : null,
        ),
      ],
    );
  }
}
